/**
 *  @author Cornelius Huang
 *  Resource fill service
 */
app.factory('resFillService', ['$http', '$q', function($http, $q) {
	baseUrl = '/DRMS/api';
	return function(dmdId, resId, capId) {
		var promise = $q(function(resolve, reject) {
			$http({
				url: baseUrl + '/DrmsDemand/updateMatchedResourceId',
				method: 'POST',
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				data: 'dmdId='+dmdId+'&resourceId='+resId+'&capId='+capId
			}).then(function(response) {
				resolve(response.data);
			}, function(response) {
				reject('resFillService - reject');
			});
		})
		return promise;
	}
}]);

app.factory('slideEmailService', ['$http', '$q', function($http, $q) {
	var baseUrl = '/DRMS/api';
	return function(mailContent) {
		var promise = $q(function(resolve, reject) {
			$http({
				url: baseUrl + '/drmsEmail/send',
				method: 'POST',
				headers: {
					'Content-Type': 'application/json'
				},
				data: mailContent
			}).then(function(response) {
				resolve(response.data);
			}, function(response) {
				reject('slideEmailService - reject');
			})
		})
		return promise;
	}
}]);

app.factory('resourceDetailService', ['$http', '$q', function($http, $q) {
	var baseUrl = '/DRMS/api';
	return function(resourceId) {
		var promise = $q(function (resolve, reject) {
			$http({
				url: baseUrl + '/DrmsNewResource/find',
				method: 'POST',
				headers: {
					'Content-Type': 'application/json'
				},
				data: {
					resourceId: resourceId,
					username: null,
					capability: null,
					designation: null,
					dateOfJoin: null,
					kinId: null,
					name: null,
					chineseName: null,
					primarySkill: null,
					status: null
				}
			}).then(function(response) {
				resolve(response.data);
			}, function() {
				reject('resourceDetailService - reject');
			})
		});
		return promise;
	}
}]);

app.factory('resCRUDService', ['$http', '$q', function($http, $q) {
	var baseUrl = '/DRMS/api/DrmsNewResource';
	return {
		resUpdate: function(resObj) {
			var promise = $q(function(resolve, reject) {
				$http({
					url: baseUrl + '/update',
					method: 'POST',
					headers: {
						'Content-Type': 'application/json'
					},
					data: resObj
				}).then(function(response) {
					resolve(response.data);
				}, function() {
					reject('resCRUDService - resUpdate reject');
				});
			});
			return promise;
		},
		resInsert: function(resObj) {
			var promise = $q(function(resolve, reject) {
				$http({
					url: baseUrl + '/add',
					method: 'POST',
					headers: {
						'Content-Type': 'application/json'
					},
					data: resObj
				}).then(function(response) {
					resolve(response.data)
				}, function() {
					reject('resCRUDService - resInsert reject');
				})
			});
			return promise;
		},
		resDelete: function(resId) {
			var promise = $q(function (resolve, reject) {
				$http({
					url: baseUrl + '/delete',
					method: 'POST',
					headers: {
						'Content-Type': 'application/json'
					},
					data: {
						resourceId: resId,
						username: null,
						capability: null,
						designation: null,
						dateOfJoin: null,
						kinId: null,
						name: null,
						chineseName: null,
						primarySkill: null,
						status: null
					}
				}).then(function(response) {
					resolve(response.data);
				}, function() {
					reject('resourceDetailService - reject');
				})
			});
			return promise;
		},
		resGet: function() {
			var promise = $q(function(resolve, reject) {
				$http({
					url: baseUrl + '/get',
					method: 'GET',
				}).then(function(response) {
					resolve(response.data);
				}, function() {
					reject('resourceDetailService - resGet reject');
				})
			});
			return promise;
		}
	}
}])